﻿using AutoMapper;
using Entity.BaseInfo.DTO.Customer;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Customer;
using Entity.Finance.DTO.IncomeAccount;
using Entity.Finance.DTO.IncomeAccountItem;
using Entity.Finance.Entity;
using Entity.Finance.View.IncomeAccount;
using Entity.Finance.View.IncomeAccountItem;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Service
{
    public class IncomeAccountService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public IncomeAccountService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取列表
         */
        public async Task<RequestResponseModel<PageResult<IncomeAccountView>>> GetIncomeAccountList(IncomeAccountSearchDTO model)
        {
            //筛选
            var query = _context.IncomeAccountView
                .WhereIF(a => a.Code.Contains(model.Code), !string.IsNullOrEmpty(model.Code))
                .WhereIF(a => a.CustomerCode.Contains(model.Customer) || a.CustomerName.Contains(model.Customer), !string.IsNullOrEmpty(model.Customer))
                ;

            //返回列表
            var list = await query
                //.Include(a=>a.IncomeToDo)
                //.OrderByDescending(a => a.CreateTime)
                .Page(model.CurrentPage, model.PageSize)
                .ToListAsync();

            //总数据量
            var count = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<IncomeAccountView>(list, count);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }
        
        /**
         * 获取列表
         */
        public async Task<RequestResponseModel<PageResult<IncomeAccountItemView>>> GetIncomeAccountItemList(IncomeAccountItemSearchDTO model)
        {
            //筛选
            var query = _context.IncomeAccountItemView
                //.WhereIF(a => a.Code.Contains(model.Code), !string.IsNullOrEmpty(model.Code))
                .WhereIF(a => a.CustomerCode.Contains(model.Customer) || a.CustomerName.Contains(model.Customer), !string.IsNullOrEmpty(model.Customer))
                .WhereIF(a => a.Status==model.Status, model.Status!=null)
                ;

            //返回列表
            var list = await query
                //.Include(a=>a.IncomeToDo)
                //.OrderByDescending(a => a.CreateTime)
                .Page(model.CurrentPage, model.PageSize)
                .ToListAsync();

            //总数据量
            var count = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<IncomeAccountItemView>(list, count);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /**
         * 新增
         */
        public async Task<RequestResponseModel<string>> AddIncomeAccount(IncomeAccountAddDTO model)
        {
            //新增数据
            var entity = _mapper.Map<IncomeAccountModel>(model);
            //entity.CreateTime = DateTime.Now;
            entity.Id = Guid.NewGuid().ToString();
            foreach (var item in model.Items)
            {
                var entityItem = _mapper.Map<IncomeAccountItemModel>(item);
                entityItem.Id = Guid.NewGuid().ToString();
                entityItem.CreateTime = DateTime.Now;
                entityItem.IncomeAccountId = entity.Id;
                entityItem.CustomerId = entity.CustomerId;
                _context.IncomeAccountItemModel.Add(entityItem);

            }
            //更新表
            _context.IncomeAccount.Add(entity);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "新增成功", entity.Id);
        }

        /**
         * 删除
         */
        public async Task<RequestResponseModel<string>> DeleteIncomeAccount(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<IncomeAccountModel>(_context.IncomeAccount, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }

            //更新表
            _context.IncomeAccount.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
